﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using System.Data;
using System.Web.Security;

namespace ProjectSem40701.Admin
{
    public partial class ArticleAdd : System.Web.UI.Page
    {
        /// <summary>
        /// Declare member variables here
        /// </summary>
        private const string SUCCESS = "success";
        private const string FAIL = "fail";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            { 
                
            }
        }
        /// <summary>
        /// Thêm tin tức mới
        /// </summary>
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
               // ArticleID = Convert.ToInt32(Request.QueryString["ArticleID"].ToString().Trim());
                
                string strUser = System.Web.HttpContext.Current.User.Identity.Name;
                string strUserID = Membership.GetUser(strUser).ProviderUserKey.ToString();
                Guid userID = new Guid(strUserID);

                System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-GB");
                DateTime dtCreatedDate = DateTime.Now;

                string strImageSource = String.Empty;

                if (this.fileUpArticleImage.HasFile)
                {
                    string str = DateTime.Now.ToString("ddMMyyyyhhmmssms");
                    this.fileUpArticleImage.SaveAs(Server.MapPath("~/images/" + str + this.fileUpArticleImage.FileName.Trim()));
                    strImageSource = str + this.fileUpArticleImage.FileName.Trim();
                }
                else
                {
                    //strImageSource = this.imgArticleImage.ImageUrl.Replace("~/images/", "");                   
                }
                
                AdminXSD.ArticleTableAdapters.tbl_ArticleTableAdapter dp = new AdminXSD.ArticleTableAdapters.tbl_ArticleTableAdapter();

                int ok = 
                    dp.tbl_Article_Insert(userID,
                    Convert.ToInt32(this.cbbArticleType.SelectedValue.Trim()),
                    dtCreatedDate, 
                    this.txtArticleTitle.Text.Trim(), 
                    strImageSource, 
                    Server.HtmlEncode(this.txtDescription.Value.Trim()),
                    Server.HtmlEncode(this.txtContent.Value.Trim()),
                    Convert.ToInt32(this.cbbArticleStatus.SelectedValue.Trim()));

                if (ok > 0)
                {
                    Show_Dialog(SUCCESS);
                }
                else
                {
                    Show_Dialog(FAIL);
                }
            }
            catch
            {
                Show_Dialog(FAIL);
            }
        }
        /// <summary>
        /// Đóng dialog
        /// </summary>
        private void Close_Dialog()
        {
            switch (this.lblSuccess.Text)
            {
                case SUCCESS:
                    Response.Redirect("Article.aspx");
                    break;
                case FAIL:
                    this.btnAdd_ModalPopupExtender.Enabled = false;
                    break;
                default:
                    this.btnAdd_ModalPopupExtender.Enabled = false;
                    break;
            }
        }
        /// <summary>
        /// Show dialog
        /// </summary>
        private void Show_Dialog(String Check)
        {
            switch (Check)
            {
                case SUCCESS:
                    this.ltrMessage.Text = "Thành công";
                    break;
                case FAIL:
                    this.ltrMessage.Text = "Thất bại";
                    break;
                default:
                    this.ltrMessage.Text = "Không thành công";
                    break;
            }
            this.lblSuccess.Text = Check;
            this.btnAdd_ModalPopupExtender.Enabled = true;
            this.btnAdd_ModalPopupExtender.Show();
        }
        /// <summary>
        /// Nút close được click
        /// </summary>
        protected void btnClose_Click(object sender, EventArgs e)
        {
            Close_Dialog();
        }

        /// <summary>
        /// Nút yes được click
        /// </summary>
        protected void btnYes_Click(object sender, EventArgs e)
        {
            Close_Dialog();
        }
    }
}
